const {
    addTypes,
    isExist,
    delTypesById,
    isNull,
    getTypes,
    getAllTypes,
    changeTypesById,
    getTypesById
} = require('../dao/typesDao')

module.exports.addTypes = async function (params) {
    const data = await isExist({
        typesName: params.typesName
    })
    console.log(data);
    if (data.length > 0) {
        return ({
            message: "该分类已存在",
            status: 2,
        })
    } else {
        const data = await addTypes(params)
        if (data._id) {
            return ({
                message: "添加成功",
                status: 1,
                data
            });
        } else {
            return ({
                message: "添加失败",
                status: 0,
            });
        }
    }
}

// module.exports.getTypes = async function () {
//     const data = await getTypes()
//     try {
//         return {
//             message: "获取成功",
//             status: 1,
//             data
//         }
//     } catch (error) {
//         return {
//             message: "获取失败",
//             status: 0,
//         }
//     }
// }

module.exports.getAllTypes = async function () {
    const data = await getAllTypes()
    try {
        return {
            message: "获取成功",
            status: 1,
            data
        }
    } catch (error) {
        return {
            message: "获取失败",
            status: 0,
        }
    }
}


module.exports.getTypesById = async function (params) {
    const data = await getTypesById(params)
    try {
        return {
            message: "获取成功",
            status: 1,
            data
        }
    } catch (error) {
        return {
            message: "获取失败",
            status: 0,
        }
    }
}

module.exports.delTypesById = async function (params) {
    const data = await isNull(params)
    let arr = data[0].insurancesData.filter(value => value.delete == false)
    if (arr.length == 0) {
        const data = await delTypesById(params)
        if (data.nModified > 0) {
            return ({
                message: "删除成功",
                status: 1,
            });
        } else {
            return ({
                message: "删除失败",
                status: 0,
            });
        }
    } else {
        return ({
            message: "该分类下产品数量不为0，不能删除",
            status: 2,

        })
    }
}



module.exports.changeTypesById = async function (params) {
    const data = await isExist({
        typesName: params.typesName
    })
    if (data.length == 0 || data.length != 0 && data[0]._id == params._id) {
        const data = await changeTypesById(params)
        console.log(data);
        if (data.nModified > 0) {
            return {
                message: "修改成功",
                status: 1,
                data
            }
        } else {
            return {
                message: "修改失败",
                status: 0,
            }
        }
    } else {
        return {
            message: "修改失败,该分类已存在",
            status: 0,
        }
    }
}

